﻿using System;
using UnityEngine;

namespace Panda.Utils
{
    public static class StringFormatUtil
    {
        public static System.String Format(System.String format, object arg0)
        {
            try
            {
                return string.Format(format, arg0);
            }
            catch (Exception e)
            {
                Debug.LogError($"Failed to format : {format}, arg0 : {arg0}, Exception : {e}");
            }
            return format;
        }

        public static System.String Format(System.String format, object arg0, object arg1)
        {
            try
            {
                return string.Format(format, arg0, arg1);
            }
            catch (Exception e)
            {
                Debug.LogError($"Failed to format : {format}, arg0 : {arg0}, arg1 : {arg1}, Exception : {e}");
            }
            return format;
        }

        public static System.String Format(System.String format, object arg0, object arg1, object arg2)
        {
            try
            {
                return string.Format(format, arg0, arg1, arg2);
            }
            catch (Exception e)
            {
                Debug.LogError($"Failed to format : {format}, arg0 : {arg0}, arg1 : {arg1}, arg2 : {arg2}, Exception : {e}");
            }
            return format;
        }

        public static System.String Format(System.String format, params object[] args)
        {
            try
            {
                return string.Format(format, args);
            }
            catch (Exception e)
            {
                for (int i = 0; i < args.Length && i < 10; i++)
                {
                    Debug.Log($"--args index:{i}, content:{args[i]}");
                }
                Debug.LogError($"Failed to format : {format}, args [before]: , Exception : {e}");
            }
            return format;
        }
        
        public static string FormatNumberWithSuffix(float stats)
        {
            if (stats >= 1_000_000_000_000_000f) return string.Format("{0:0.##Q}", (stats / 1_000_000_000_000_000f));
            else if (stats >= 1_000_000_000_000f) return string.Format("{0:0.##T}", (stats / 1_000_000_000_000f));
            else if (stats >= 1_000_000_000f) return string.Format("{0:0.##B}", (stats / 1_000_000_000f));
            else if (stats >= 1_000_000f) return string.Format("{0:0.##M}", (stats / 1_000_000f));
            else if (stats >= 10_000f) return string.Format("{0:0.##k}", (stats / 1_000f));
            else return string.Format("{0:0}", stats);
        }
    }
}